package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

func main() {
	comment := "Go is an open source programming language that makes it easy to build simple, " +
		"reliable,  " +
		"and efficient software."
	basicReader := strings.NewReader(comment)
	reader := bufio.NewReader(basicReader)
	delimiter := byte(',')
	line, err := reader.ReadSlice(delimiter)
	if err != nil {
		fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
	}
	fmt.Printf("ReadSlice读取(%d): %q\n", len(line), line)
	fmt.Println("缓冲区里的未读字节数：", reader.Buffered())
	fmt.Println()

	delimiter = byte('!')  // 读不到这个分隔符
	line, err = reader.ReadSlice(delimiter)
	if err != nil {
		fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
	}
	fmt.Printf("ReadSlice读取(%d): %q\n", len(line), line)
	fmt.Println("缓冲区里的未读字节数：", reader.Buffered())
	fmt.Println()

	basicReader.Reset(comment)
	reader2 := bufio.NewReaderSize(basicReader, 80)
	delimiter = byte('!')  // 读不到这个分隔符
	line, err = reader2.ReadSlice(delimiter)
	if err != nil {
		fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
	}
	fmt.Printf("ReadSlice读取(%d): %q\n", len(line), line)
	fmt.Println("缓冲区里的未读字节数：", reader2.Buffered())
}
